package com.ctg.behavior.calc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.impala.calculator.CtUSCalculator;
import com.ctg.behavior.common.calc.CtCalcResult;
import com.ctg.behavior.common.usersequence.CtUSCalcRequest;
import com.ctg.behavior.common.usersequence.CtUSCalcResult;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/users")
public class CtUSReportController {

	@Autowired
	private CtUSCalculator calculator;

	@PostMapping("/report/sequence")
	public CtUSCalcResult calcReport(@RequestBody CtUSCalcRequest request) throws Exception {

		CtUSCalcResult calcResult = new CtUSCalcResult();
		log.info("body={}", JSON.toJSONString(request.getReq()));
		CtCalcResult result = calculator.doCalculator(request.getReq());
		log.info("result={}", JSON.toJSONString(result));
		
		if (!(result instanceof CtUSCalcResult)) {
			log.error("doCalculatorResult class={}", result.getClass());
			log.error("doCalculatorResult={}", JSON.toJSONString(result));
			return calcResult;
		}
		calcResult = (CtUSCalcResult) result;
		return calcResult;

	}

}
